package com.service.scheduler.sched;

import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.TriggerKey;

import com.service.cache.CacheManager;
import com.service.other.Logger;
import com.service.other.ServerConfig;
import com.service.scheduler.job.OfflineJob;
import com.service.scheduler.server.SchedulerCreator;

/**
 * 离线检查调度器
 * @author lj
 *
 */
public class OfflineSched {

	private Scheduler sched;
	
	public void init() {
		try {
			sched = SchedulerCreator.createScheduler(this.getClass().getSimpleName(), 5, Trigger.DEFAULT_PRIORITY);
			initSched();
			CacheManager.instance().schedMap.put(sched.getSchedulerName(),sched);
		} catch (Exception e) {
			Logger.err(e);
		}
	}
	
	private void initSched() throws SchedulerException {
		JobKey jobKey = SchedulerCreator.createJobKey("checkoffline", "offline");
		TriggerKey triggerKey = SchedulerCreator.createTriggerKey("checkoffline","offline");
		// 每隔 time 分钟执行一次
		int time = ServerConfig.instance().offLineTime / 2;
		String cronExpr = SchedulerCreator.createMinuteExpr(time);
		JobDetail job = SchedulerCreator.createJob(jobKey, OfflineJob.class);
		CronTrigger cronTrigger = SchedulerCreator.createCronTrigger(triggerKey,cronExpr);
		sched.scheduleJob(job, cronTrigger);
	}
	
}
